package com.tigercel.xiangchengyun.utils

import android.app.Activity
import android.text.TextUtils
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.TextView
import com.tigercel.xiangchengyun.R

class TitleBuilder {

    private var viewTitle: View? = null
    private var tvTitle: TextView? = null
    private var imgTitle: ImageView? = null
    private var ivLeft: ImageView? = null
    private var ivRight: ImageView? = null
    private var tvLeft: TextView? = null
    private var tvRight: TextView? = null

    constructor(context: Activity) {
        viewTitle = context.findViewById(R.id.rl_titlebar)
        tvTitle = viewTitle!!.findViewById<View>(R.id.titlebar_tv) as TextView
        ivLeft = viewTitle!!.findViewById<View>(R.id.titlebar_iv_left) as ImageView
        imgTitle = viewTitle!!.findViewById<View>(R.id.titlebar_img) as ImageView
        ivRight = viewTitle!!.findViewById<View>(R.id.titlebar_iv_right) as ImageView
        tvLeft = viewTitle!!.findViewById<View>(R.id.titlebar_tv_left) as TextView
        tvRight = viewTitle!!.findViewById<View>(R.id.titlebar_tv_right) as TextView
    }

    constructor(context: View) {
        viewTitle = context.findViewById(R.id.rl_titlebar)
        imgTitle = viewTitle!!.findViewById<View>(R.id.titlebar_img) as ImageView
        tvTitle = viewTitle!!.findViewById<View>(R.id.titlebar_tv) as TextView
        ivLeft = viewTitle!!.findViewById<View>(R.id.titlebar_iv_left) as ImageView
        ivRight = viewTitle!!.findViewById<View>(R.id.titlebar_iv_right) as ImageView
        tvLeft = viewTitle!!.findViewById<View>(R.id.titlebar_tv_left) as TextView
        tvRight = viewTitle!!.findViewById<View>(R.id.titlebar_tv_right) as TextView
    }

    // title

    fun setTitleBgRes(resid: Int): TitleBuilder {
        viewTitle!!.setBackgroundResource(resid)
        return this
    }

    fun setTitleText(text: String): TitleBuilder {
        tvTitle!!.visibility = if (TextUtils.isEmpty(text))
            View.GONE
        else
            View.VISIBLE
        tvTitle!!.text = text
        return this
    }

    // left

    fun setLeftImage(resId: Int): TitleBuilder {
        ivLeft!!.visibility = if (resId > 0) View.VISIBLE else View.GONE
        ivLeft!!.setImageResource(resId)
        return this
    }

    fun setLeftText(text: String): TitleBuilder {
        tvLeft!!.visibility = if (TextUtils.isEmpty(text)) View.GONE else View.VISIBLE
        tvLeft!!.text = text
        return this
    }

    fun setLeftOnClickListener(listener: OnClickListener): TitleBuilder {
        if (ivLeft!!.visibility == View.VISIBLE) {
            ivLeft!!.setOnClickListener(listener)
        } else if (tvLeft!!.visibility == View.VISIBLE) {
            tvLeft!!.setOnClickListener(listener)
        }
        return this
    }

    // right

    fun setRightImage(resId: Int): TitleBuilder {
        ivRight!!.visibility = if (resId > 0) View.VISIBLE else View.GONE
        ivRight!!.setImageResource(resId)
        return this
    }

    fun setRightText(text: String): TitleBuilder {
        tvRight!!.visibility = if (TextUtils.isEmpty(text))
            View.GONE
        else
            View.VISIBLE
        tvRight!!.text = text
        return this
    }

    fun setRightOnClickListener(listener: OnClickListener): TitleBuilder {
        if (ivRight!!.visibility == View.VISIBLE) {
            ivRight!!.setOnClickListener(listener)
        } else if (tvRight!!.visibility == View.VISIBLE) {
            tvRight!!.setOnClickListener(listener)
        }
        return this
    }

}
